<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">订单管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col :span="9">
          <el-input
            placeholder="请输入内容"
            v-model="queryData.query"
            class="input-with-select"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="queryOrder"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderTableData" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
          width="400px"
        ></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template v-slot="{ row }">
            <el-tag v-if="row.pay_status === 1" type="success">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="下单时间" width="180px">
          <template v-slot="{ row }">{{
            row.create_time | formatDate
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showEditadressDialog"
            ></el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location"
              @click="showProgressDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 6, 7, 8]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 编辑地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="adress1">
          <el-cascader
            v-model="addressForm.adress1"
            :options="cityDate"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="详细地址" prop="adress2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <span>这是一段信息</span>
    </el-dialog>
  </div>
</template>

<script>
import cityDate from './citydata.js'

export default {
  data() {
    return {
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 5,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: ''
      },
      orderTableData: [],
      total: 0,
      // 控制修改地址对话框的显示和隐藏
      addressVisible: false,
      addressForm: {
        // 级联选择器选中的地址索引
        adress1: [],
        adress2: ''
      },
      // 地址规则验证对象
      addressFormRules: {
        adress1: [
          {
            required: true,
            message: '请选者省市区/县',
            trigger: 'blur'
          }
        ],
        adress2: [
          {
            required: true,
            message: '请填写详细地址',
            trigger: 'blur'
          }
        ]
      },
      cityDate,
      // 控制物流进度对话框的显示和隐藏
      progressVisible: false,
      // 物流信息
      progressData: []
    }
  },
  created() {
    this.getOrderData()
  },
  methods: {
    async getOrderData() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryData
      })

      if (res.meta.status !== 200) {
        return this.$message.error('获取订单数据列表失败')
      }
      this.$message.success('获取订单数据列表成功')
      this.orderTableData = res.data.goods
      console.log(res)
      this.total = res.data.total
    },
    // 点击查询按钮查询订单
    queryOrder() {
      this.getOrderData()
    },
    // 展示修改地址的对话框
    showEditadressDialog() {
      this.addressVisible = true
    },
    // 编辑地址对话框关闭时触发
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
      this.addressForm.address2 = ''
    },
    // 展示物流进度对话框
    async showProgressDialog() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('查看物流信息失败')
      }
      console.log(res)
      this.$message.success('查看物流信息成功')
      this.progressData = res.data
      this.progressVisible = true
    },
    handleSizeChange(size) {
      console.log(size)
      this.queryData.pagesize = size
      this.getOrderData()
    },
    handleCurrentChange(curr) {
      console.log(curr)
      this.queryData.pagenum = curr
      this.getOrderData()
    }
  }
}
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 20px;
}

.el-row {
  margin-bottom: 20px;
}

.el-cascader {
  width: 100%;
}

.el-pagination {
  margin-top: 10px;
}
</style>
